package com.havilahshawn.finaltest22ndsesforad.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.havilahshawn.finaltest22ndsesforad.R;
import com.havilahshawn.finaltest22ndsesforad.dao.Student;

import java.util.List;

/**
 * Created by My Family on 2018/6/28.
 */

public class StuAdapter extends BaseAdapter{

    private  List<Student> students;
    private  Context context;

    public StuAdapter(Context context,List<Student> students) {
        this.context = context;
        this.students = students;
    }

    @Override
    public int getCount() {
        return students.size();
    }

    @Override
    public Object getItem(int i) {
        return students.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if(view == null){
            view = LayoutInflater.from(context).inflate(R.layout.list_item,viewGroup,false);
            viewHolder = new ViewHolder();

            Student student = students.get(i);
            viewHolder.nameTextView = view.findViewById(R.id.tv_name);
            viewHolder.ageTextView = view.findViewById(R.id.tv_age);
            viewHolder.sexTextView = view.findViewById(R.id.tv_sex);
            viewHolder.delImageButton = view.findViewById(R.id.ib_del);

            viewHolder.nameTextView.setText(student.getName());
            viewHolder.ageTextView.setText(String.valueOf(student.getAge()));
            viewHolder.sexTextView.setText(String.valueOf(student.getSex()));

            viewHolder.delImageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });

            view.setTag(viewHolder);

        }else {
            view.getTag();
        }
        notifyDataSetChanged();
        return view;
    }

    public void setData(List<Student> data) {
        notifyDataSetChanged();
        if(data != null) {
            students.addAll(data);
        }
    }

    static class ViewHolder {
        TextView nameTextView;
        TextView ageTextView;
        TextView sexTextView;
        ImageButton delImageButton;
    }

    }
